package com.swanclouds.redfish.systems;

import com.google.common.base.Preconditions;
import com.swanclouds.api.systems.VolumeService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.model.systems.Volume;
import com.swanclouds.model.systems.VolumeCreate;
import com.swanclouds.model.systems.VolumeInitType;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseSystemService;
import com.swanclouds.redfish.systems.domain.VolumeModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 逻辑盘集合资源
 *
 * @author 张立强
 */
public class VolumeServiceImpl extends BaseSystemService implements VolumeService {

    /**
     * 查询逻辑盘集合资源信息
     *
     * @return CollectionResource
     */
    @Override
    public CollectionResource get(String storageId) {
        Preconditions.checkNotNull(storageId, "Storage Id id null");
        return get(CollectionResourceModel.class,uri(ClientConstants.SYSTEMS_STORAGES_VOLUMES, getSystemId(), storageId)).execute();
    }

    /**
     * 查询指定逻辑盘资源信息
     *
     * @param storageId 存储控制器ID
     * @param volumeId 逻辑盘ID
     * @return Volume
     */
    @Override
    public Volume getVolume(String storageId, String volumeId) {
        Preconditions.checkNotNull(storageId, "Storage Id id null");
        Preconditions.checkNotNull(volumeId,"Volume Id is null");
        return get(VolumeModel.class,uri(ClientConstants.SYSTEMS_STORAGES_VOLUMES_ID, getSystemId(), storageId, volumeId)).execute();
    }


    /**
     * 查询逻辑盘资源信息列表
     *
     * @param storageId 存储控制器ID
     * @return List&lt;Volume&gt;
     */
    @Override
    public List<Volume> list(String storageId) {
        Preconditions.checkNotNull(storageId, "Storage Id is null");
        List<Volume> volumeList = new ArrayList<>();
        CollectionResource resource = get(storageId);
        resource.getCollectionResourceList().stream().forEach(ref -> {
            volumeList.add(get(VolumeModel.class,uri(ref)).execute());
        });
        return volumeList;
    }

    @Override
    public ActionResponse createVolume(VolumeCreate volumeCreate,String storageId) {
        Preconditions.checkNotNull(volumeCreate, "Volume is null");
        Preconditions.checkNotNull(storageId, "Storage Id id null");
        return postWithResponse(uri(ClientConstants.SYSTEMS_STORAGES_VOLUMES, getSystemId(), storageId)).entity(volumeCreate).execute();
    }

    /**
     * 删除指定逻辑盘资源
     *
     * @param storageId 存储控制器ID
     * @param volumeId 逻辑盘ID
     * @return ActionResponse
     */
    @Override
    public ActionResponse delete(String storageId, String volumeId) {
        Preconditions.checkNotNull(storageId, "Storage Id id null");
        Preconditions.checkNotNull(volumeId,"Volume Id is null");
        return deleteWithResponse(uri(ClientConstants.SYSTEMS_STORAGES_VOLUMES_ID, getSystemId(), storageId, volumeId)).execute();
    }

    /**
     * 初始化指定逻辑盘资源
     *
     * @param storageId 存储控制器ID
     * @param volumeId 逻辑盘ID
     * @param type      [QuickInit,FullInit,CancelInit]
     * @return ActionResponse
     */
    @Override
    public ActionResponse initVolume(String storageId, String volumeId, VolumeInitType type) {
        Preconditions.checkNotNull(storageId, "Storage Id id null");
        Preconditions.checkNotNull(volumeId,"Volume Id is null");
        Preconditions.checkNotNull(type,"Init type is null");
        return postWithResponse(uri(ClientConstants.SYSTEMS_STORAGES_VOLUMES_ID_INIT, getSystemId(), storageId, volumeId))
                .entity(type)
                .execute();
    }
}
